package service

import (
	"context"
	"github.com/spf13/cast"
	"nearby/biz/common"
	"nearby/biz/dal/db/repo"
	"nearby/biz/model"
)

type DeleteMomentService struct {
}

func (ss *DeleteMomentService) Execute(ctx context.Context, req *model.DeleteMomentRequest) (*model.DeleteMomentResponse, error) {
	// 1 权限校验
	user := common.GetUser(ctx)
	if user == nil {
		return nil, common.NewBizErr(common.EvilViewErrCode, "用户未登陆", nil)
	}
	momentRepo := repo.NewMomentRepo(ctx)
	momentPo, err := momentRepo.GetMoment(ctx, repo.GetMomentRequest{
		MomentID: cast.ToInt64(req.MomentID),
	})
	if err != nil {
		return nil, common.NewBizErr(common.EvilViewErrCode, "查询数据库失败", err)
	}
	if momentPo.UserID != user.UserID {
		return nil, common.NewBizErr(common.EvilViewErrCode, "删除的过程中好像有点问题", err)
	}

	// 2 删除
	err = momentRepo.DeleteMoment(ctx, cast.ToInt64(req.MomentID))
	if err != nil {
		return nil, common.NewBizErr(common.BizErrCode, "删除动态失败", err)
	}

	return &model.DeleteMomentResponse{
		Meta: common.MetaOk,
	}, nil
}
